import imp
from importlib.resources import path
import shutil
import random
import os
import re


file_path='data/train0'
save_path='data/test0'

shutil.copy
random.seed(0)
path_dir=os.listdir(os.path.join(file_path,"images"))
path_dir=sorted(path_dir,key=lambda x:" ".join(re.findall("\d+",x)),reverse=True)

rate=0.15
copy_num=int(len(path_dir)*rate)
sample=random.sample(path_dir,copy_num)

for name in sample:
# for name in path_dir:

    assert(os.path.exists(os.path.join(file_path,"images",name)),name)
    assert(os.path.exists(os.path.join(file_path,"gts",name)),name)
    assert(os.path.exists(os.path.join(file_path,"mask",name)),name)

    
    if(os.path.exists(os.path.join(file_path,"images",name)) and \
        os.path.exists(os.path.join(file_path,"gts",name)) and \
         os.path.exists(os.path.join(file_path,"mask",name))) :
        shutil.move(os.path.join(file_path,"images",name),os.path.join(save_path,"images",name))
        shutil.move(os.path.join(file_path,"gts",name),os.path.join(save_path,"gts",name))
        shutil.move(os.path.join(file_path,"mask",name),os.path.join(save_path,"mask",name))


    # print(name)
print("success!")